# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from . import models

from one.models import Article
from django.core.paginator import Paginator
# Create your views here.
# #返回内容
# def index(request):
# 	#return HttpResponse('Hello,world!')
# 	return render(request, 'blog/index.html')

#显示列表信息
def index(request):
	#return HttpResponse('Hello,world!')
	# #article =models.Article.objects.get(pk=1) #显示单条信息
	# #return render(request, 'blog/index.html',{'article':article})
	# articles = models.Article.objects.all()
	# return render(request, 'blog/index.html',{'articles':articles})
	return render(request, 'onet/index.html')

def article_content(request):
	article =Article.objects.all()[0]
	title   =article.title
	brief_content=article.brief_content
	content =article.content
	publish_date=article.publish_date
	request_str=(title,
				  brief_content,
				  content,
				  publish_date)
	return HttpResponse(request_str)


#新闻文章全部内容 分页内容
def article_index(request):
	page =request.GET.get('page')
	if page:
	   page =int(page)
	else:
	   page =1
	#print(page)
	all_article =Article.objects.all()
	paginator   =Paginator(all_article,1)
	page_num    =paginator.num_pages
	page_article_list =paginator.page(page)
	if page_article_list.has_next():
	    next_page =page + 1
	else:
	    next_page=page
	if page_article_list.has_previous():
		previous_page =page - 1
	else:
		previous_page =page
    #右边内容
	article_r =Article.objects.all()
	return render(request,'onet/article_index.html',
		                  {'all_article':page_article_list,
		                   'article_r':article_r,
		                   'page_num':range(1,page_num + 1 ),
		                   'curr_page':page,
		                   'next_page':next_page,
		                   'previous':previous_page})
#新闻详情
def article_info(request,article_id):
	article_info=Article.objects.get(pk=article_id)
	return render(request,'onet/article_info.html',{'article_info':article_info})
